from app import db
from datetime import datetime

class Message(db.Model):
    __tablename__ = 'messages'
    
    id = db.Column(db.BigInteger, primary_key=True)
    user_id = db.Column(db.BigInteger, nullable=False)  # 去除外键约束
    content = db.Column(db.Text, nullable=False)
    is_user = db.Column(db.Boolean, default=True)  # True表示用户消息，False表示AI回复
    conversation_id = db.Column(db.String(50), nullable=False)  # 对话ID，用于分组
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    def to_dict(self):
        return {
            'id': self.id,
            'user_id': self.user_id,
            'content': self.content,
            'is_user': self.is_user,
            'conversation_id': self.conversation_id,
            'created_at': self.created_at.isoformat()
        } 